home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-06 | 8.5 KB | 342 lines | [TEXT/MPCC] |
- // ===========================================================================
- // LServerActions.cp The Server Actions window
- // ===========================================================================
- //
- // This program is NOT necessarily the model of good programming. The attention
- // has been spent on the server side. However, if you wish to use this
- // as a starter program, feel free.
- //
- // This program was written with Metrowerks C/C++ for 68k, using the
- // PowerPlant class hierarchy.
- //
- // It was written by Nick Brosnahan and Jamie Osborne. 9/94
- //
- // Apple Computer, Inc. makes no warranties about the the fitness of this code
- // for any purpose and will not be held liable for any damages you may suffer
- // from using this code. Furthermore, Apple Computer, Inc. will not be held
- // liable for the death of Elvis or any other space alien.
- //
- // Your mileage may vary.
-
- #include "LServerActions.h"
- #include "NetLister.h"
- #include <LStdControl.h>
- #include <LCaption.h>
- #include <LEditField.h>
- #include <PP_Messages.h>
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
-
- #include <stdlib.h>
- #include <stdio.h>
-
- // additions
- typedef AddrBlock ATAddress;
-
- #include <string.h>
-
- const MessageT cmd_DoGetName = 1011;
- const MessageT cmd_DoCloseConn = 1012;
- const MessageT cmd_DoCurrConn = 1013;
- const MessageT cmd_DoPeakConn = 1014;
-
- enum {kRequestOpen = 1, kRequestName, kRequestCurCon, kRequestPeakCon, kRequestQuit };
-
- const int kRespBufferSize = 256;
-
- // ---------------------------------------------------------------------------
- // • CreateAppWindowStream [static]
- // ---------------------------------------------------------------------------
- // Return a new AppWindow object initialized using data from a Stream
-
- LServerActions*
- LServerActions::CreateServerActionsStream(
- LStream *inStream)
- {
- return (new LServerActions(inStream));
- }
-
-
- // ---------------------------------------------------------------------------
- // • LServerActions
- // ---------------------------------------------------------------------------
- // Default Constructor
-
- LServerActions::LServerActions()
- {
- mSocket = -1;
- mGetName = mCloseConn = mCurrConn = mPeakConn = NULL;
- mResult1 = NULL;
- mIsConnected = FALSE;
- mRespBuffer = NULL;
- }
-
-
- // ---------------------------------------------------------------------------
- // • LServerActions(SWindowInfo&)
- // ---------------------------------------------------------------------------
- // Construct AppWindow from the data in a struct
-
- LServerActions::LServerActions(
- SWindowInfo &inWindowInfo)
- : LWindow(inWindowInfo)
- {
- mSocket = -1;
- mGetName = mCloseConn = mCurrConn = mPeakConn = NULL;
- mResult1 = NULL;
- mIsConnected = FALSE;
- mRespBuffer = NULL;
- }
-
-
- // ---------------------------------------------------------------------------
- // • LServerActions(ResIDT, Uint32, LCommander*)
- // ---------------------------------------------------------------------------
- // Construct a AppWindow from a WIND Resource with the specified attributes
- // and SuperCommander
- //
- // Side Effect: Created window becomes the current port
-
- LServerActions::LServerActions(
- ResIDT inWINDid,
- Uint32 inAttributes,
- LCommander *inSuper)
- : LWindow(inWINDid, inAttributes, inSuper)
- {
- mSocket = -1;
- mGetName = mCloseConn = mCurrConn = mPeakConn = NULL;
- mResult1 = NULL;
- mIsConnected = FALSE;
- mRespBuffer = NULL;
- }
-
-
- // ---------------------------------------------------------------------------
- // • LServerActions(LStream*)
- // ---------------------------------------------------------------------------
- // Construct a AppWindow from the data in a Stream
-
- LServerActions::LServerActions(
- LStream *inStream)
- : LWindow(inStream)
- {
- mSocket = -1;
- mGetName = mCloseConn = mCurrConn = mPeakConn = NULL;
- mResult1 = NULL;
- mIsConnected = FALSE;
- mRespBuffer = NULL;
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~LServerActions
- // ---------------------------------------------------------------------------
- // Destructor
-
- LServerActions::~LServerActions()
- {
- if (mRespBuffer)
- free (mRespBuffer);
- }
-
-
- void LServerActions::AttemptClose()
- {
- // Try to close the connection, but don't bother checking for the error code
- DoRequest(mSocket, &mServerAddress, kRequestQuit, mRespBuffer, kRespBufferSize);
- LWindow::AttemptClose();
- }
-
- void LServerActions::ListenToMessage(
- MessageT inMessage,
- void *ioParam)
- {
- int stat;
-
- switch (inMessage)
- {
- case cmd_DoGetName:
- stat = DoRequest(mSocket, &mServerAddress, kRequestName, mRespBuffer, kRespBufferSize);
- c2pstr(mRespBuffer);
- mResult1->SetDescriptor((ConstStr255Param) mRespBuffer);
- mResult1->Refresh();
- break;
-
- case cmd_DoCloseConn:
- stat = DoRequest(mSocket, &mServerAddress, kRequestQuit, mRespBuffer, kRespBufferSize);
- c2pstr(mRespBuffer);
- mResult1->SetDescriptor((ConstStr255Param) mRespBuffer);
- mResult1->Refresh();
- if (stat != -1)
- SetConnected(FALSE);
- break;
- case cmd_DoCurrConn:
- stat = DoRequest(mSocket, &mServerAddress, kRequestCurCon, mRespBuffer, kRespBufferSize);
- c2pstr(mRespBuffer);
- mResult1->SetDescriptor((ConstStr255Param) mRespBuffer);
- mResult1->Refresh();
- break;
- case cmd_DoPeakConn:
- stat = DoRequest(mSocket, &mServerAddress, kRequestPeakCon, mRespBuffer, kRespBufferSize);
- c2pstr(mRespBuffer);
- mResult1->SetDescriptor((ConstStr255Param) mRespBuffer);
- mResult1->Refresh();
- break;
- default:
- if (GetSuperCommander() != nil)
- GetSuperCommander()->ProcessCommand(inMessage, ioParam);
- break;
- }
- }
-
- void LServerActions::DoSetupServerActions(ATAddress addr)
- {
- mServerAddress = addr;
- mGetName = (LStdButton *)this->FindPaneByID('act1');
- mCloseConn = (LStdButton *)this->FindPaneByID('act2');
- mCurrConn = (LStdButton *)this->FindPaneByID('act3');
- mPeakConn = (LStdButton *)this->FindPaneByID('act4');
-
- mGetName->AddListener(this);
- mCloseConn->AddListener(this);
- mCurrConn->AddListener(this);
- mPeakConn->AddListener(this);
-
- mResult1 = (LCaption *)this->FindPaneByID('rlt1');
-
- mRespBuffer = (char *) malloc(kRespBufferSize);
- mSocket = GetSocket();
-
- return;
- }
-
- void LServerActions::DoClient()
- {
- int result;
-
- result = DoRequest(mSocket, &mServerAddress, kRequestOpen, mRespBuffer, kRespBufferSize);
- c2pstr(mRespBuffer);
- mResult1->SetDescriptor((ConstStr255Param) mRespBuffer);
-
- if (result != -1)
- {
- mServerAddress.aSocket = result;
- SetConnected(TRUE);
- }
- }
-
- unsigned int GetSocket()
- {
- ATPParamBlock pb;
- int stat;
-
- memset(&pb, 0, sizeof(pb));
- stat = POpenATPSkt(&pb, false);
- Assert_(!stat);
- return pb.ATPatpSocket;
- }
-
- int DoRequest(unsigned socket, ATAddress *addr, int request, void *buffer, int bufferLen)
- {
- char requestBuff[64];
- int len;
-
- ATPParamBlock pb;
- BDSType bds;
- int numResps;
- OSErr stat;
-
- if (request == kRequestOpen)
- strcpy(requestBuff, "Hello Server, from the Client.");
- else
- strcpy(requestBuff, "Here is a request.");
-
- len = strlen(requestBuff) + 1;
-
- numResps = BuildBDS((Ptr) buffer, (Ptr)&bds, (short) bufferLen);
-
- memset(&pb, 0, sizeof(pb));
- pb.ATPuserData = (long) request;
- pb.ATPatpSocket = socket;
- pb.ATPatpFlags = atpXOvalue;
- pb.ATPaddrBlock = *addr;
- pb.ATPreqLength = len;
- pb.ATPreqPointer = (Ptr)requestBuff;
- pb.ATPbdsPointer = (Ptr)&bds;
- pb.ATPnumOfBuffs = numResps == 1;
- pb.ATPtimeOutVal = 6;
- pb.ATPretryCount = 2;
-
- stat = PNSendRequest(&pb, false);
-
- if (stat != 0)
- sprintf((char *)buffer, "Received error from PNSendRequest: %d", (int)stat);
-
- return bds[0].userBytes;
-
- }
-
- int SendATRequest(unsigned socket, ATAddress *dest, void *reqBuff,
- int reqSize, void *respBuff, int respBuffLen, int whichRequest)
- {
- ATPParamBlock pb;
- BDSType bds;
- int numResps;
- OSErr stat;
- short myNode, myNet;
-
- stat = GetNodeAddress(&myNode,&myNet);
-
- numResps = BuildBDS((Ptr) respBuff, (Ptr)&bds, (short) respBuffLen);
-
- memset(&pb, 0, sizeof(pb));
- pb.ATPuserData = (long) whichRequest;
- pb.ATPatpSocket = socket;
- pb.ATPatpFlags = atpXOvalue;
- pb.ATPaddrBlock = *dest;
- pb.ATPreqLength = reqSize;
- pb.ATPreqPointer = (Ptr)reqBuff;
- pb.ATPbdsPointer = (Ptr)&bds;
- pb.ATPnumOfBuffs = numResps == 1;
- pb.ATPtimeOutVal = 6;
- pb.ATPretryCount = 2; // ***
-
- stat = PNSendRequest(&pb, false);
-
- if (stat != 0)
- {
- sprintf((char *)respBuff, "Received error from PNSendRequest: %d", (int)stat);
- return -1;
- }
-
- return bds[0].userBytes;
- }
-
- void LServerActions::SetConnected(Boolean connected)
- {
- if (connected)
- {
- mGetName->Enable();
- mCloseConn->Enable();
- mCurrConn->Enable();
- mPeakConn->Enable();
- }
- else
- {
- mGetName->Disable();
- mCloseConn->Disable();
- mCurrConn->Disable();
- mPeakConn->Disable();
- mResult1->SetDescriptor("\p Not Connected.");
- }
- mIsConnected = connected;
- Refresh();
- }
-
-